package com.umanji.page;

import java.util.*;

public class Page {
	protected int pageNum;						
	protected int pageCount;					
	protected int pageSize = 10;				
	protected int pageListSize = 5;				
	protected String pageURL;					
	protected PageButton nextButton = new PageButtonText("[다음]");
	protected PageButton prevButton = new PageButtonText("[이전]");
	protected String pageNumName = "pageNum";
	protected String keysURL;
	protected int maxRecordCount;
	
	

	protected HashMap<String, String> keys = new HashMap<String, String>();
	
	public Page(String pageURL) {
		this.pageURL = pageURL;
	}
	public Page(String pageURL, String pageNum) {
		this.pageURL = pageURL;
		this.pageNum = convPageNum(pageNum);
	}
	public Page(String pageURL, String pageNum, int pageSize) {
		this.pageURL = pageURL;
		this.pageNum = convPageNum(pageNum);
		this.pageSize = pageSize;
	}

	public Page(String pageURL, String pageNum, int pageSize, int pageListSize) {
		this.pageURL = pageURL;
		this.pageNum = convPageNum(pageNum);
		this.pageSize = pageSize;
		this.pageListSize = pageListSize;
	}
	
	public HashMap<String, Object> getParameterMap() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		map.put("startPos", (pageNum-1)*pageSize + 1 );
		map.put("endPos", pageNum*pageSize);
		
		if( keys.size() > 0 ) {
			Iterator<String> iKeys = keys.keySet().iterator();
			while( iKeys.hasNext() ) {
				String key = iKeys.next();
				map.put(key, keys.get(key));
			}
		}
		
		return map;
	}
	
	private int convPageNum(String pageNum) {
		if( pageNum == null || pageNum.trim().length() < 1 )
			return 1;
		
		return Integer.parseInt( pageNum );
	}
	

	public void addKey(String key, String value) 
	{
		if( value != null && value.trim().length() > 0 )
			keys.put(key, value);
	}
	
	
	public String getList() 
	{
		StringBuilder buf = new StringBuilder();
		
		makeKeysURL();
		
		buf.append( makePrevButtonTag() );
		buf.append( makePageListTag() );
		buf.append( makeNextButtonTag() );		
		
		return buf.toString();
	}
	
	public int getStartPage() {
		return pageNum - ((pageNum-1) % pageListSize);
	}
	
	private String makePrevButtonTag() {
		StringBuilder buf = new StringBuilder();
		
		if( (getStartPage()-pageListSize) > 0 ) {
			buf.append( makeAnchorTag(getStartPage()-1) );
			buf.append( prevButton.getName() ).append("</a>&nbsp;");
		}
		
		return buf.toString();
	}
	
	private String makeNextButtonTag() {
		StringBuilder buf = new StringBuilder();
		
		if( (getStartPage()+pageListSize) <= pageCount ) {
			buf.append( makeAnchorTag(getStartPage()+pageListSize) );
			buf.append( nextButton.getName() ).append("</a>&nbsp;");
		}
		
		return buf.toString();
	}
	
	
	private String makePageListTag() {		
		StringBuilder buf = new StringBuilder();		
		int size = getStartPage()+pageListSize;
		
		for(int i = getStartPage(); i < size; i++) 
		{
			if( i == pageNum )
				buf.append("&nbsp;").append(i).append("&nbsp;&nbsp;");
			else {
				buf.append( makeAnchorTag(i));
				buf.append("[").append(i).append("]</a>&nbsp;");
			}
			if( i >= pageCount ) break;
		}
		
		return buf.toString();
	}
	
	
	private String makeAnchorTag(int pageNum) {
		StringBuilder buf = new StringBuilder();
		
		buf.append("<a href='").append(pageURL).append("?").append(pageNumName).append("=")
			.append(pageNum).append( getKeysURL() ).append("'>");
		
		return buf.toString();
	}
	
	
	
	private void makeKeysURL() {
		StringBuilder buf = new StringBuilder();

		if( keys.size() > 0 )
		{
			Iterator<String> iKeys = keys.keySet().iterator();
			while( iKeys.hasNext() ) {
				String key = iKeys.next();
				buf.append("&")
					.append(key)
					.append("=")
					.append( keys.get(key) );
			}
		}
		
		keysURL = buf.toString();
	}

	
	public String getKeysURL() {
		if( keysURL == null ) makeKeysURL();
		
		return keysURL;
	}
	
	
	public void setMaxRecordCount(int maxCount) {
		this.maxRecordCount = maxCount;
		
		if( maxCount%getPageSize()== 0 )
			setPageCount( maxCount/getPageSize() );
		else
			setPageCount( maxCount/getPageSize()+1 );
	}
	
	public int getMaxRecordCount() {
		return maxRecordCount;
	}
		
	public void setPageNumName(String pageNumName) {
		this.pageNumName = pageNumName;
	}	
	public String getPageNumName() {
		return pageNumName;
	}	
	public int getPageListSize() {
		return pageListSize;
	}
	public void setPageListSize(int pageListSize) {
		this.pageListSize = pageListSize;
	}
	public String getPageURL() {
		return pageURL;
	}
	public void setPageURL(String pageURL) {
		this.pageURL = pageURL;
	}
	public HashMap<String, String> getKeys() {
		return keys;
	}
	public void setKeys(HashMap<String, String> keys) {
		this.keys = keys;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(String pageNum) {
		this.pageNum = convPageNum(pageNum);
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public PageButton getNextButton() {
		return nextButton;
	}
	public void setNextButton(PageButton nextButton) {
		this.nextButton = nextButton;
	}
	public PageButton getPrevButton() {
		return prevButton;
	}
	public void setPrevButton(PageButton prevButton) {
		this.prevButton = prevButton;
	}
}
